/**
 * Copyright 2009-2010 - YangJiandong(chunquedong)
 * 
 * This file is part of ChunMap project
 * Licensed under the GNU LESSER GENERAL PUBLIC LICENSE(Version >=3)
 * 你可以自由复制、传播本项目的下载包文件，但必须保持其完整性。
 * 我们不用对使用中的风险及由此造成的损失承担任何责任。
 * 详细情况请见《ChunMap许可协议》。

 * 想了解更多有关ChunMap的信息，请访问http://code.google.com/p/chunmap/
 */
package chunmap.model.operate.overlay;

import java.util.ArrayList;
import java.util.List;

import chunmap.model.geom.MultiPolygon;
import chunmap.model.geom.Polygon;

/**
 * @author chunquedong
 * 
 */
public class OverLayOp {

	public MultiPolygon computeIntersect(Polygon r1, Polygon r2) {
		FragmentCollection fragmentCollection = new FragmentCollection(r1, r2);
		List<Polygon> rs = fragmentCollection.getRing(Fragment.Inner,
				Fragment.Inner);

		return makeMultiPolygon(rs);
	}

	private MultiPolygon makeMultiPolygon(List<Polygon> rs) {
		if (rs.size() == 0)
			return null;
		List<Polygon> pgs = new ArrayList<Polygon>();
		for (Polygon r : rs) {
			pgs.add(r);
		}
		return new MultiPolygon(pgs);
	}

	public MultiPolygon computeUnion(Polygon r1, Polygon r2) {
		FragmentCollection fragmentCollection = new FragmentCollection(r1, r2);
		List<Polygon> rs = fragmentCollection.getRing(Fragment.Outer,
				Fragment.Outer);

		return makeMultiPolygon(rs);
	}

	public MultiPolygon computeDifference(Polygon r1, Polygon r2) {
		FragmentCollection fragmentCollection = new FragmentCollection(r1, r2);
		List<Polygon> rs = fragmentCollection.getRing(Fragment.Outer,
				Fragment.Inner);

		return makeMultiPolygon(rs);
	}
}
